﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ThiTracNghiem
{
    public partial class FormLogin : Form
    {
        bool valid = false;
        String ConnectionString = Program.ConnectionString;
        public FormLogin()
        {
            InitializeComponent();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Dispose();
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (cbType.SelectedIndex == 0)
            {
                if (!valid)
                {
                    if (txtMaSV.Text.Trim().Equals(""))
                    {
                        return;
                    }
                    ConnectionString = Program.ConnectionString;
                    ConnectionString += "User ID=sv;Password=12345";
                    SqlConnection con = new SqlConnection(ConnectionString);
                    SqlCommand cmd = new SqlCommand("SP_GET_SV", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@MASV", SqlDbType.Char).Value = txtMaSV.Text.Trim();
                    try
                    {
                        con.Open();
                        SqlDataReader reader = cmd.ExecuteReader();

                        if (reader.Read())
                        {
                            txtHoTen.Text = reader[1].ToString() + " " + reader[2].ToString();
                            DateTime date = DateTime.Parse(reader[3].ToString().Trim());
                            txtNgaySinh.Text = date.Day.ToString("00") + "/" + date.Month.ToString("00") + "/" + date.Year;
                            valid = true;
                        }
                        else
                        {
                            MessageBox.Show("Mã sinh viên không tồn tại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        Program.ConnectionString = ConnectionString;
                        Program.CurrentUser = "sv";
                        Program.StatusInfo = txtHoTen.Text + "     |     Mã sinh viên  " + txtMaSV.Text + "      |     Ngày sinh " + txtNgaySinh.Text;

                        this.DialogResult = DialogResult.OK;
                        this.Dispose();
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("Lỗi không xác định.\nChi tiết: " + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    finally
                    {
                        con.Close();
                    }
                }
                else
                {
                    
                    Program.ConnectionString = ConnectionString;
                    Program.CurrentUser = "sv";
                    Program.StatusInfo = txtHoTen.Text + "     |     Mã sinh viên  " + txtMaSV.Text + "      |     Ngày sinh " + txtNgaySinh.Text;

                    this.DialogResult = DialogResult.OK;
                    
                    this.Dispose();
                }
            }
            else
            {
                String loginName = txtUserName.Text.Trim();
                String password = txtPassword.Text.Trim();

                ConnectionString = Program.ConnectionString;
                ConnectionString += "User ID=" + loginName + ";Password=" + password;
                SqlConnection con = new SqlConnection(ConnectionString);
                SqlCommand cmd = new SqlCommand("SP_GET_GV", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@LOGIN", SqlDbType.Char).Value = txtUserName.Text;
                try
                {
                    con.Open();
                    String info = "";
                    String user = "";
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        info = reader[1].ToString().Trim();
                        info += " " + reader[2].ToString().Trim();
                        user = reader[0].ToString().Trim();
                    }
                    else
                    {
                        throw new Exception("Tên đăng nhập không tồn tại.");
                    }
                    
                    Program.ConnectionString = ConnectionString;
                    Program.CurrentUser = user;
                    Program.StatusInfo = info;

                    this.DialogResult = DialogResult.OK;
                    this.Dispose();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Sai tên đăng nhập hoặc mật khẩu. Mã lỗi: " + ex.Number, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Lỗi không xác định.\nChi tiết: " + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                finally
                {
                    con.Close();
                }
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnDangNhap_Click(null, null);
            }
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {
            cbType.SelectedIndex = 0;
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (txtMaSV.Text.Trim().Equals(""))
            {
                return;
            }
            ConnectionString = Program.ConnectionString;
            ConnectionString += "User Id=sv;Password=12345";
            SqlConnection con = new SqlConnection(ConnectionString);
            SqlCommand cmd = new SqlCommand("SP_GET_SV", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@MASV", SqlDbType.Char).Value = txtMaSV.Text.Trim();
            try
            {
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    txtHoTen.Text = reader[1].ToString() + " " + reader[2].ToString();
                    DateTime date = DateTime.Parse(reader[3].ToString().Trim());
                    txtNgaySinh.Text = date.Day.ToString("00") + "/" + date.Month.ToString("00") + "/" + date.Year;
                    valid = true;
                }
                else
                {
                    MessageBox.Show("Mã sinh viên không tồn tại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Sai tên đăng nhập hoặc mật khẩu.\nChi tiết: " + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            finally
            {
                con.Close();
            }
        }

        private void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbType.SelectedIndex == 0)
            {
                pnGV.Visible = false;
                btnDangNhap.Text = "Vào thi";
            }
            else
            {
                pnGV.Visible = true;
                btnDangNhap.Text = "Đăng nhập";
            }
        }

        private void txtMaSV_TextChanged(object sender, EventArgs e)
        {
            txtHoTen.Text = "";
            txtNgaySinh.Text = "";
            valid = false;
        }

        private void txtMaSV_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = ValidateInput(e);
        }

        public static bool ValidateInput(KeyPressEventArgs e)
        {
            bool blDiscardUserInput = true;

            if (Char.IsLetter(e.KeyChar))
                return false;

            if (Char.IsNumber(e.KeyChar))
                return false;

            // Backspace or Space
            if (e.KeyChar == 8)
                blDiscardUserInput = false;

            return blDiscardUserInput;
        }

        private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = ValidateInputLogin(e);
        }

        public static bool ValidateInputLogin(KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
                return false;

            if (Char.IsNumber(e.KeyChar))
                return false;

            // Backspace or Space
            if (e.KeyChar == 8)
                return false;

            return true;
        }

        public static bool ValidateInputPassword(KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
                return false;

            if (Char.IsNumber(e.KeyChar))
                return false;

            // Backspace or Space
            if (e.KeyChar == 8)
                return false;

            return true;
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = ValidateInputPassword(e);
        }
    }
}
